%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Creates variables for VAR(k) with trend
% this version: November 2010

% input:
% y is data
% c constant or trend (c=0: no constant; c=1: constant)
% k is number of lags

% output:
% yy LHS variables
% xx RHS variables
% x  RHS variables, differently stacked

function [yy,xx,x] = VARvariables(y,c,k)

s = size(y);    % size of dataset
T = s(1);       % length of sample
N = s(2);       % number of variables

for i=1:N,
    yy(:,i)=y(k+1:T,i); % LHS; keep lags for regressors
    for j=1:k,
        xx(:,k*(i-1)+j)=y(k+1-j:T-j,i); % RHS regressors
        % stacking all lags of first variable, then all lags of second,
        % etc.
    end; 
end;
if c==1
    xx=[ones(T-k,1) xx];
end

% sorting differently
for ij = 1:k
    zz(:,(ij-1)*N+1:N*ij) = xx(:,ij+c:k:size(xx,2));
    % stacking lag one of all variables, then lag two, etc.
    % ij+c:k:size(xx,2) takes out every kth element up to the full size
    % starting after constant
end
if c==0 
    x=zz;
else
    z(:,1:c)=xx(:,1:c); % constant stays the same
    x = [z zz];
end

end